﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.Engine;
using KCMS.BusinessLogic.Models;
using KCMS.Models;

namespace KCMS.Controllers
{
    public class ProductFilterController : Controller
    {
        private IBrendService brendService = ObjectFactory.GetInstance<IBrendService>();
        private GenderListGenerator<Brend> brendListGenerator = new GenderListGenerator<Brend>();
        private IProductService productService = ObjectFactory.GetInstance<IProductService>();
        private IProductCategoryService productCategoryService = ObjectFactory.GetInstance<IProductCategoryService>();
        [HttpGet]
        public ActionResult Filter(Guid categoryId,Guid brendId)
        {

            var brends = (from t in productService.getAllPublished()
                          where t.CategoryId.Equals(categoryId)
                          group t by t.Brend into gruping
                          select gruping.Key).ToList();
            ProductFilter filter = new ProductFilter
            {
                CategoryId = categoryId,
                BrendList = brendListGenerator.getGenderListWithEmptyOption(brends, brendId)
            };
            return PartialView(filter);
        }
        [HttpPost]
        public ActionResult Filter(ProductFilter model)
        {
            var category = productCategoryService.getById(model.CategoryId);
            try
            {
                var brend = brendService.getById(model.BrendId);
                return RedirectToAction("Show", "ProductCategory", new { code = category.Code, brend = brend.Code });
            }
            catch
            {
                return RedirectToAction("Show", "ProductCategory", new { code = category.Code });
            }
         }

    }
}
